"""
Copyright (c) 2023 - present SKbioinfo
"""
from flask_restful import Resource
from flask import jsonify,request,current_app
from apps.API.utils.log_helper import lg
from apps.API.connector.db_common import *
from flask_jwt_extended import jwt_required
from apps.API.common.common_time import *
from apps.API.pipeExecutor.util import executor_cmd
from apps.API.pipeExecutor.models import SampleSheet
from apps import db

class interfaceBasicFormInfoByDb(Resource):
    def __init__(self):
        self.executor=executor_cmd()
    @jwt_required()
    def get(self,name):
        try:
            page=int(request.args.get('page'))
            limit=int(request.args.get('limit'))
            query=SampleSheet.query.order_by(SampleSheet.created_at.desc())
            pagination=query.paginate(page=page, per_page=limit, error_out=False)
            total_length=query.count()
            results=[item.to_dict() for item in pagination.items]
            response=jsonify({"total":total_length,"data":results})
            response.status=202
            return response
        except Exception as e:
            response=jsonify({})
            response.status=404
            lg.error(e)
            return response
        
    @jwt_required()    
    def put(self,name):
        try:
            for item in request.json["data"]:
                SampleSheet.query.filter(SampleSheet.id==item["id"]).update(item)
            db.session.commit()
            response=jsonify({})
            response.status=202
            return response
        except Exception as e:
            lg.error(e)
            response=jsonify({})
            response.status=404
            return response